import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:psi/theme/app_colors.dart';

class TextWidget extends StatelessWidget {
  final String? data;
  final Color color;
  final double? fontSize;
  final FontWeight fontWeight;
  final double? lineHeight;
  final TextAlign textAlign;
  final int? maxLines;
  final TextOverflow overflow;
  final bool? softWrap;
  final TextDecoration? decoration;
  final TextBaseline? textBaseline;
  final double? letterSpacing;
  final FontStyle? fontStyle;
  final TextStyle? style;

  const TextWidget(
    this.data, {
    Key? key,
    this.color = const Color(0xFF333333),
    this.fontSize,
    this.fontWeight = FontWeight.normal,
    this.lineHeight,
    this.textAlign = TextAlign.left,
    this.maxLines,
    this.overflow = TextOverflow.clip,
    this.softWrap,
    this.decoration,
    this.textBaseline,
    this.letterSpacing,
    this.fontStyle,
    this.style,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text(
      data ?? '',
      overflow: overflow,
      style: style ??
          TextWidget.createTextStyle(
            color: color,
            fontSize: fontSize,
            fontWeight: fontWeight,
            lineHeight: lineHeight,
            decoration: decoration,
            textBaseline: textBaseline,
            letterSpacing: letterSpacing,
            fontStyle: fontStyle,
          ),
      softWrap: softWrap,
      textAlign: textAlign,
      maxLines: maxLines,
    );
  }

  static TextStyle createTextStyle({
    Color color = Colors.black,
    double? fontSize,
    FontWeight fontWeight = FontWeight.normal,
    double? lineHeight,
    TextDecoration? decoration,
    TextBaseline? textBaseline,
    double? letterSpacing = 0,
    FontStyle? fontStyle,
  }) {
    fontSize ??= 14.sp;
    double? height = lineHeight == null ? null : (lineHeight / fontSize);

    return TextStyle(
      color: color,
      fontSize: fontSize,
      fontWeight: fontWeight,
      height: height,
      decoration: decoration,
      textBaseline: textBaseline,
      letterSpacing: letterSpacing,
      fontStyle: fontStyle,
    );
  }
}

class PlaceHolderText extends StatelessWidget {
  final String? data;
  final String? placeholder;
  final TextStyle? styleValue;
  final TextStyle? stylePlaceholder;

  const PlaceHolderText(this.data, {Key? key, this.placeholder, this.styleValue, this.stylePlaceholder}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextWidget(
      (data != null && data!.isNotEmpty) ? data : placeholder,
      style: (data != null && data!.isNotEmpty)
          ? styleValue ??
              TextWidget.createTextStyle(
                color: primaryColor,
                fontSize: 14.sp,
              )
          : stylePlaceholder ??
              TextWidget.createTextStyle(
                fontSize: 14.sp,
                color: const Color(0xFF999999),
              ),
    );
  }
}
